package com.zhuge_new.Base;

import android.app.Activity;
import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.os.IBinder;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.text.Editable;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.zhuge_new.R;
import com.zhuge_new.untils.ActivityStack;
import com.zhuge_new.untils.StatusBarUtil;

import net.idik.lib.slimadapter.SlimAdapter;

/**
 * Created by lenovo on 2018-08-24.
 */

public class BaseActivity extends AppCompatActivity {

    private Toolbar toolbar;

    public Activity baseContext;

    public SlimAdapter mAdapter;

    public int pageNum=1;

    public boolean isLoadingMore;

    public int mPosition;
    private TextView tvTitle;
    private ImageView ivBack;
    private ImageView ivRight;

    @Override
    protected void onCreate( Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_base);
        initToolbar();
        baseContext=this;
        ActivityStack.getScreenManager().pushActivity(this);
    }

    private void initToolbar() {
        StatusBarUtil.setStatusBarLightMode(this);
        StatusBarUtil.setStatusBarColor(this,R.color.white);
        toolbar = findViewById(R.id.toolbar);
        if (toolbar != null) {
            setSupportActionBar(toolbar);
            ActionBar actionBar = getSupportActionBar();
            if (actionBar != null)
                actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
        }
    }

    //初始化控件
    public void init_title(){
        tvTitle = findViewById(R.id.tv_nav_title);
        ivBack = findViewById(R.id.iv_nav_back);
        ivRight = findViewById(R.id.iv_nav_right);
    }

    //初始化控件，改变中间标题
    public void init_title(int title) {
        init_title();

    }



    private void changeTitle(String title) {
        if (tvTitle == null) tvTitle = findViewById(R.id.tv_nav_title);
        assert tvTitle != null;
        tvTitle.setText(title);
    }


    public void transparentStatusBar(boolean isToolbarVisible) {
        transparentStatusBar();
        setToolbarVisibility(isToolbarVisible);
    }

    public void setToolbarVisibility(boolean isVisible) {
        if (toolbar != null) toolbar.setVisibility(isVisible ? View.VISIBLE : View.GONE);
    }

    public void transparentStatusBar() {if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        WindowManager.LayoutParams localLayoutParams = getWindow().getAttributes();
        localLayoutParams.flags = (WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS | localLayoutParams.flags);
    }


    }

    @Override
    public void setContentView(View view) {
        LinearLayout rootLayout=findViewById(R.id.context_layout);
        if (rootLayout ==null)return;
        rootLayout.addView(view, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        initToolbar();
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        if (ev.getAction() == MotionEvent.ACTION_DOWN) {
            // 获得当前得到焦点的View，一般情况下就是EditText（特殊情况就是轨迹求或者实体案件会移动焦点）
            View v = getCurrentFocus();
            if (isShouldHideInput(v, ev)) {
                hideSoftInput(v.getWindowToken());
            }
        }
        return super.dispatchTouchEvent(ev);
    }

    /**
     * 根据EditText所在坐标和用户点击的坐标相对比，来判断是否隐藏键盘，因为当用户点击EditText时没必要隐藏
     */
    private boolean isShouldHideInput(View v, MotionEvent event) {
        if (v != null && (v instanceof EditText)) {
            int[] l = {0, 0};
            v.getLocationInWindow(l);
            int left = l[0], top = l[1], bottom = top + v.getHeight(), right = left
                    + v.getWidth();
            return !(event.getX() > left && event.getX() < right
                    && event.getY() > top && event.getY() < bottom);
        }
        // 如果焦点不是EditText则忽略，这个发生在视图刚绘制完，第一个焦点不在EditView上，和用户用轨迹球选择其他的焦点
        return false;
    }

    /**
     * 多种隐藏软件盘方法的其中一种
     */
    private void hideSoftInput(IBinder token) {
        if (token != null) {
            InputMethodManager im = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
            if (im == null) return;
            im.hideSoftInputFromWindow(token, InputMethodManager.HIDE_NOT_ALWAYS);
        }
    }


}
